home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / term43-source.lha / Extras / Source / gtlayout-Source.lha / LT_Build.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-18  |  12.3 KB  |  593 lines

  1. /*  GadTools layout toolkit
  2. **
  3. **  Copyright © 1993-1994 by Olaf `Olsen' Barthel
  4. **  Freely distributable.
  5. */
  6.  
  7. #include "gtlayout_global.h"
  8.  
  9. struct Window * __stdargs
  10. LT_Build(LayoutHandle *Handle,...)
  11. {
  12.     struct Window    *Result;
  13.     va_list      VarArgs;
  14.  
  15.     va_start(VarArgs,Handle);
  16.     Result = LT_BuildA(Handle,(struct TagItem *)VarArgs);
  17.     va_end(VarArgs);
  18.  
  19.     return(Result);
  20. }
  21.  
  22. struct Window * LIBENT
  23. LT_BuildA(REG(a0) LayoutHandle *handle,REG(a1) struct TagItem *TagParams)
  24. {
  25.     LONG         left, top, width, height;
  26.     struct IBox     newBounds;
  27.     struct Menu    *menu;
  28.     struct TagItem    *item;
  29.     LONG         placeLeft;
  30.     LONG         placeTop;
  31.     struct IBox     zoomBox;
  32.     struct IBox    *zoom;
  33.     LONG         i;
  34.     struct TextFont *font;
  35.     struct TagItem    *NewTags = NULL,*Tags;
  36.     BOOLEAN      BlockParent = handle -> BlockParent,
  37.              SmartZoom = FALSE;
  38.     struct Window    *Parent = handle -> Parent;
  39.     STRPTR         title        = NULL;
  40.     struct IBox    *bounds     = NULL;
  41.     LONG         extraWidth    = 0,
  42.              extraHeight    = 0;
  43.     ULONG         IDCMP        = NULL;
  44.     UWORD         align        = 0;
  45.     WORD         MinX,
  46.              MinY,
  47.              MaxX,
  48.              MaxY;
  49.     BOOL         SizeGadget    = FALSE,
  50.              SizeBRight    = FALSE,
  51.              SizeBBottom    = FALSE;
  52.     ULONG         BorderBottom,
  53.              BorderRight;
  54.  
  55.     if(!handle)
  56.         return(NULL);
  57.  
  58.     /* ALWAYS */
  59.     {
  60.         struct TagItem *TagList = (struct TagItem *)TagParams;
  61.  
  62.         while(item = NextTagItem(&TagList))
  63.         {
  64.             switch((ULONG)item -> ti_Tag)
  65.             {
  66.                 case LAWN_TitleText:
  67.  
  68.                     title = (STRPTR)item -> ti_Data;
  69.                     break;
  70.  
  71.                 case LAWN_TitleID:
  72.  
  73.                     if(!handle -> LocaleHook)
  74.                         return(NULL);
  75.                     else
  76.                         title = (STRPTR)CallHookPkt(handle -> LocaleHook,handle,(APTR)item -> ti_Data);
  77.  
  78.                     break;
  79.  
  80.                 case LAWN_Bounds:
  81.  
  82.                     bounds = (struct IBox *)item -> ti_Data;
  83.                     break;
  84.  
  85.                 case LAWN_ExtraWidth:
  86.  
  87.                     extraWidth = item -> ti_Data;
  88.                     break;
  89.  
  90.                 case LAWN_ExtraHeight:
  91.  
  92.                     extraHeight = item -> ti_Data;
  93.                     break;
  94.  
  95.                 case LAWN_IDCMP:
  96.  
  97.                     IDCMP = item -> ti_Data;
  98.                     break;
  99.  
  100.                 case LAWN_AlignWindow:
  101.  
  102.                     align = item -> ti_Data;
  103.                     break;
  104.             }
  105.         }
  106.     }
  107.  
  108.     if(!bounds)
  109.     {
  110.         LTP_GetDisplayClip(handle -> Screen,&newBounds . Left,&newBounds . Top,&newBounds . Width,&newBounds . Height);
  111.  
  112.         bounds = &newBounds;
  113.     }
  114.  
  115.     if(title)
  116.         top = handle -> Screen -> WBorTop + handle -> Screen -> Font -> ta_YSize + 1 + handle -> InterHeight;
  117.     else
  118.         top = handle -> Screen -> WBorTop + handle -> InterHeight;
  119.  
  120.     left = handle -> Screen -> WBorLeft + handle -> InterWidth;
  121.  
  122.     BorderRight    = handle -> Screen -> WBorRight;
  123.     BorderBottom    = handle -> Screen -> WBorBottom;
  124.  
  125.     if(handle -> ResizeView)
  126.     {
  127.         if(handle -> ResizeView -> Special . List . ResizeX)
  128.             BorderRight = LTP_GetSizeWidth(handle);
  129.  
  130.         if(handle -> ResizeView -> Special . List . ResizeY)
  131.             BorderBottom = LTP_GetSizeHeight(handle);
  132.     }
  133.  
  134.     LTP_CreateGadgets(handle,bounds,left,top,left + handle -> InterWidth + BorderRight,top + handle -> InterHeight + BorderBottom);
  135.  
  136.     if(handle -> Failed)
  137.         return(NULL);
  138.  
  139.     width    = left + handle -> TopGroup -> Width + handle -> InterWidth + BorderRight;
  140.     height    = top + handle -> TopGroup -> Height + handle -> InterHeight + BorderBottom;
  141.  
  142.     if(handle -> ResizeView)
  143.     {
  144.         MaxX = MinX = width;
  145.         MaxY = MinY = height;
  146.  
  147.         if(handle -> ResizeView -> Special . List . ResizeX)
  148.             MaxX = handle -> Screen -> Width;
  149.  
  150.         if(handle -> ResizeView -> Special . List . ResizeY)
  151.         {
  152.             MaxY = handle -> Screen -> Height;
  153.  
  154.             SizeBBottom = TRUE;
  155.         }
  156.         else
  157.             SizeBRight = TRUE;
  158.  
  159.         SizeGadget = TRUE;
  160.  
  161.         if(handle -> ResizeView -> Special . List . MinChars && handle -> ResizeView -> Special . List . MinChars < handle -> ResizeView -> Chars)
  162.             MinX -= handle -> GlyphWidth * (handle -> ResizeView -> Chars - handle -> ResizeView -> Special . List . MinChars);
  163.  
  164.         if(handle -> ResizeView -> Special . List . MinLines && handle -> ResizeView -> Special . List . MinChars < handle -> ResizeView -> Lines)
  165.             MinY -= handle -> RPort . TxHeight * (handle -> ResizeView -> Lines - handle -> ResizeView -> Special . List . MinLines);
  166.     }
  167.     else
  168.     {
  169.         MaxX = MinX = width;
  170.         MaxY = MinY = height;
  171.     }
  172.  
  173.     if(align & ALIGNF_LEFT)
  174.         placeLeft = 0;
  175.     else
  176.     {
  177.         if(align & ALIGNF_RIGHT)
  178.             placeLeft = bounds -> Width - (width + extraWidth);
  179.         else
  180.             placeLeft = (bounds -> Width - (width + extraWidth)) / 2;
  181.     }
  182.  
  183.     if(align & ALIGNF_TOP)
  184.         placeTop = 0;
  185.     else
  186.     {
  187.         if(align & ALIGNF_BOTTOM)
  188.             placeTop = bounds -> Height - (height + extraHeight);
  189.         else
  190.             placeTop = (bounds -> Height - (height + extraHeight)) / 2;
  191.     }
  192.  
  193.     if(align & ALIGNF_EXTRA_LEFT)
  194.         placeLeft += extraWidth;
  195.     else
  196.     {
  197.         if(!(align & ALIGNF_EXTRA_RIGHT))
  198.             placeLeft += extraWidth / 2;
  199.     }
  200.  
  201.     if(align & ALIGNF_EXTRA_TOP)
  202.         placeTop += extraHeight;
  203.     else
  204.     {
  205.         if(!(align & ALIGNF_EXTRA_BOTTOM))
  206.             placeTop += extraHeight / 2;
  207.     }
  208.  
  209.     placeLeft    += bounds -> Left;
  210.     placeTop    += bounds -> Top;
  211.  
  212.     if(placeLeft < 0)
  213.         placeLeft = 0;
  214.  
  215.     if(placeTop < 0)
  216.         placeTop = 0;
  217.  
  218.     menu = NULL;
  219.     zoom = NULL;
  220.  
  221.     handle -> AutoRefresh = TRUE;
  222. #ifdef DO_CLONING
  223.     if(handle -> CloneExtra)
  224.     {
  225.         placeLeft    = 0;
  226.         placeTop    = handle -> CloneExtra -> Screen -> BarHeight + 1;
  227.  
  228.         zoomBox . Left    = placeLeft;
  229.         zoomBox . Top    = placeTop;
  230.     }
  231. #endif    /* DO_CLONING */
  232.  
  233.     /* ALWAYS */
  234.     {
  235.         struct TagItem *TagList = (struct TagItem *)TagParams;
  236.  
  237.         while(item = NextTagItem(&TagList))
  238.         {
  239.             switch((ULONG)item -> ti_Tag)
  240.             {
  241.                 case LA_Menu:
  242.  
  243.                     handle -> IDCMP |= IDCMP_MENUPICK;
  244.  
  245.                     menu = (struct Menu *)item -> ti_Data;
  246.  
  247.                     break;
  248.  
  249.                 case LAWN_SmartZoom:
  250.  
  251.                     SmartZoom = item -> ti_Tag;
  252.  
  253.                     if(!SmartZoom)
  254.                         break;
  255.  
  256.                 case LAWN_Zoom:
  257.  
  258.                     if(item -> ti_Data)
  259.                     {
  260.                         if(title && SmartZoom)
  261.                         {
  262.                             STATIC UWORD WhichTable[3] =
  263.                             {
  264.                                 CLOSEIMAGE,
  265.                                 ZOOMIMAGE,
  266.                                 DEPTHIMAGE
  267.                             };
  268.  
  269.                             LONG     Size = 0,i;
  270.                             BOOLEAN  GotIt = TRUE;
  271.                             UWORD     SizeType;
  272.                             Object    *Image;
  273.  
  274.                             if(handle -> Screen -> Flags & SCREENHIRES)
  275.                                 SizeType = SYSISIZE_MEDRES;
  276.                             else
  277.                                 SizeType = SYSISIZE_LOWRES;
  278.  
  279.                             for(i = 0 ; i < 3 ; i++)
  280.                             {
  281.                                 if(Image = NewObject(NULL,SYSICLASS,
  282.                                     SYSIA_Size,    SizeType,
  283.                                     SYSIA_Which,    WhichTable[i],
  284.                                     SYSIA_DrawInfo, handle -> DrawInfo,
  285.                                 TAG_DONE))
  286.                                 {
  287.                                     ULONG Width;
  288.  
  289.                                     GetAttr(IA_Width,Image,&Width);
  290.  
  291.                                     Size += Width;
  292.  
  293.                                     DisposeObject(Image);
  294.                                 }
  295.                                 else
  296.                                     GotIt = FALSE;
  297.                             }
  298.  
  299.                             if(GotIt)
  300.                             {
  301.                                 Size += 8 + TextLength(&handle -> Screen -> RastPort,title,strlen(title)) + 8;
  302.  
  303.                                 zoomBox . Width = Size;
  304.                             }
  305.                             else
  306.                                 zoomBox . Width = width;
  307.                         }
  308.                         else
  309.                             zoomBox . Width = width;
  310.  
  311.                         if(V39)
  312.                         {
  313.                             zoomBox . Left    = -1;
  314.                             zoomBox . Top    = -1;
  315.                         }
  316.                         else
  317.                         {
  318.                             zoomBox . Left    = placeLeft;
  319.                             zoomBox . Top    = placeTop;
  320.                         }
  321.  
  322.                         zoomBox . Height = handle -> Screen -> WBorTop + handle -> Screen -> Font -> ta_YSize + 1;
  323.  
  324.                         zoom = &zoomBox;
  325.                     }
  326.  
  327.                     break;
  328.  
  329.                 case LAWN_UserPort:
  330.  
  331.                     handle -> MsgPort = (struct MsgPort *)item -> ti_Data;
  332.  
  333.                     break;
  334.  
  335.                 case LAWN_HelpHook:
  336.  
  337.                     handle -> HelpHook = (struct Hook *)item -> ti_Data;
  338.  
  339.                     break;
  340.  
  341.                 case LAWN_Left:
  342.  
  343.                     placeLeft = item -> ti_Data;
  344.  
  345.                     break;
  346.  
  347.                 case LAWN_Top:
  348.  
  349.                     placeTop = item -> ti_Data;
  350.  
  351.                     break;
  352.  
  353.                 case LAWN_Parent:
  354.  
  355.                     Parent = (struct Window *)item -> ti_Data;
  356.                     break;
  357.  
  358.                 case LAWN_BlockParent:
  359.  
  360.                     BlockParent = item -> ti_Data;
  361.                     break;
  362.  
  363.                 case LAWN_BelowMouse:
  364.  
  365.                     if((placeLeft = handle -> Screen -> MouseX - (width / 2)) < 0)
  366.                         placeLeft = 0;
  367.  
  368.                     if((placeTop = handle -> Screen -> MouseY - (height / 2)) < 0)
  369.                         placeTop = 0;
  370.  
  371.                     break;
  372.  
  373.                 case LAWN_MaxPen:
  374.  
  375.                     handle -> MaxPen = (LONG)item -> ti_Data;
  376.                     break;
  377.  
  378.                 case LAWN_MoveToWindow:
  379.  
  380.                     handle -> MoveToWindow = (LONG)item -> ti_Data;
  381.                     break;
  382.  
  383.                 case LAWN_AutoRefresh:
  384.  
  385.                     handle -> AutoRefresh = (LONG)item -> ti_Data;
  386.                     break;
  387.             }
  388.         }
  389.     }
  390.  
  391.     Tags = (struct TagItem *)TagParams;
  392.  
  393.     if(handle -> BackgroundPen && !V39)
  394.     {
  395.         if(NewTags = CloneTagItems(Tags))
  396.         {
  397.             STATIC Tag Filter[] = { WA_SimpleRefresh,TAG_DONE };
  398.  
  399.             FilterTagItems(NewTags,Filter,TAGFILTER_NOT);
  400.  
  401.             Tags = NewTags;
  402.         }
  403.         else
  404.             return(NULL);
  405.     }
  406.  
  407.     if(Parent)
  408.     {
  409.         WORD    Left,Top,Width,Height,MoveLeft,MoveTop,
  410.             WindowLeft,WindowTop,WindowWidth,WindowHeight;
  411.  
  412.         WindowLeft    = Parent -> LeftEdge + Parent -> BorderLeft;
  413.         WindowTop    = Parent -> TopEdge + Parent -> BorderTop;
  414.         WindowWidth    = Parent -> Width - (Parent -> BorderLeft + Parent -> BorderRight);
  415.         WindowHeight    = Parent -> Height - (Parent -> BorderTop + Parent -> BorderBottom);
  416.  
  417.         LTP_GetDisplayClip(Parent -> WScreen,&Left,&Top,&Width,&Height);
  418.  
  419.         if((MoveLeft = WindowLeft + (WindowWidth - width) / 2) < 0)
  420.             MoveLeft = 0;
  421.  
  422.         if((MoveTop = WindowTop + (WindowHeight - height) / 2) < 0)
  423.             MoveTop = 0;
  424.  
  425.         if(MoveLeft < Left || MoveLeft + width > Left + Width)
  426.             MoveLeft = -1;
  427.  
  428.         if(MoveTop < Top || MoveTop + height > Top + Height)
  429.             MoveTop = -1;
  430.  
  431.         if(MoveTop != -1 && MoveLeft != -1)
  432.         {
  433.             placeLeft    = MoveLeft;
  434.             placeTop    = MoveTop;
  435.         }
  436.     }
  437.  
  438.     if(BlockParent && Parent)
  439.     {
  440.         LT_LockWindow(Parent);
  441.  
  442.         handle -> Parent = Parent;
  443.     }
  444.  
  445.     if(SizeGadget)
  446.         zoom = NULL;
  447.  
  448.     if(handle -> Window = OpenWindowTags(NULL,
  449.         WA_Left,        placeLeft,
  450.         WA_Top,         placeTop,
  451.         WA_Width,        width,
  452.         WA_Height,        height,
  453.         WA_NewLookMenus,    TRUE,
  454.         WA_CustomScreen,    handle -> Screen,
  455.         WA_BackFill,        &handle -> BackfillHook,
  456.         WA_MinWidth,        MinX,
  457.         WA_MinHeight,        MinY,
  458.         WA_MaxWidth,        MaxX,
  459.         WA_MaxHeight,        MaxY,
  460.         WA_SizeGadget,        SizeGadget,
  461.         WA_SizeBBottom,        SizeBBottom,
  462.         WA_SizeBRight,        SizeBRight,
  463.  
  464.         zoom    ? WA_Zoom :            TAG_IGNORE,    zoom,
  465.         !handle -> MsgPort ? WA_IDCMP :     TAG_IGNORE,    IDCMP_REFRESHWINDOW | IDCMP_RAWKEY | IDCMP_ACTIVEWINDOW | IDCMP_INACTIVEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP | handle -> IDCMP,
  466.         title    ? WA_Title :            TAG_IGNORE,    title,
  467.         handle -> AmigaGlyph ? WA_AmigaKey :    TAG_IGNORE,    handle -> AmigaGlyph,
  468.         handle -> CheckGlyph ? WA_Checkmark :    TAG_IGNORE,    handle -> CheckGlyph,
  469.     TAG_MORE,Tags))
  470.     {
  471.         font = handle -> RPort . Font;
  472.  
  473.         handle -> RPort = *handle -> Window -> RPort;
  474.  
  475.         LTP_SetFont(handle,font);
  476.  
  477.         AddGList(handle -> Window,handle -> List,(UWORD)-1,(UWORD)-1,NULL);
  478.  
  479.         RefreshGList(handle -> List,handle -> Window,NULL,(UWORD)-1);
  480.  
  481.         if(NewTags)
  482.         {
  483.             FreeTagItems(NewTags);
  484.  
  485.             NewTags = NULL;
  486.         }
  487.  
  488.         if(V39)
  489.         {
  490.             if(handle -> MaxPen > 0)
  491.             {
  492.                 SetMaxPen(&handle -> RPort,handle -> MaxPen);
  493.                 SetMaxPen(handle -> Window -> RPort,handle -> MaxPen);
  494.             }
  495.         }
  496.  
  497.         GT_RefreshWindow(handle -> Window,NULL);
  498.         LTP_DrawGroup(handle,handle -> TopGroup);
  499.  
  500.         LTP_MoveToWindow(handle);
  501.  
  502.         if(handle -> MsgPort)
  503.         {
  504.             handle -> Window -> UserPort = handle -> MsgPort;
  505.  
  506.             if(!ModifyIDCMP(handle -> Window,IDCMP_REFRESHWINDOW | IDCMP_RAWKEY | IDCMP_INACTIVEWINDOW | IDCMP_ACTIVEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP | handle -> IDCMP))
  507.             {
  508.                 handle -> Window -> UserPort = NULL;
  509.  
  510.                 return (NULL);
  511.             }
  512.         }
  513.  
  514.         if(menu)
  515.             SetMenuStrip(handle -> Window,menu);
  516.  
  517.         handle -> Previous = NULL;
  518.  
  519.         for(i = 0 ; i < handle -> Count ; i++)
  520.         {
  521.             if(handle -> GadgetArray[i])
  522.             {
  523.                 ObjectNode *node;
  524.  
  525.                 if(GETOBJECT(handle -> GadgetArray[i],node))
  526.                 {
  527.                     if(LIKE_STRING_KIND(node) || (node -> Type == INTEGER_KIND))
  528.                     {
  529.                         if(!handle -> Previous)
  530.                             handle -> Previous = handle -> GadgetArray[i];
  531.                     }
  532.                 }
  533.             }
  534.         }
  535. #ifdef DO_CLONING
  536.         if(handle -> CloneExtra)
  537.             ScreenToFront(handle -> Window -> WScreen);
  538. #endif
  539.     }
  540.     else
  541.     {
  542.         if(handle -> Parent)
  543.         {
  544.             LT_UnlockWindow(handle -> Parent);
  545.  
  546.             handle -> Parent = NULL;
  547.         }
  548.     }
  549.  
  550.     FreeTagItems(NewTags);
  551.  
  552.     return(handle -> Window);
  553. }
  554.  
  555.  
  556. /*****************************************************************************/
  557.  
  558.  
  559. struct Window * __stdargs
  560. LT_Layout(LayoutHandle *handle, STRPTR title,struct IBox *bounds,LONG extraWidth, LONG extraHeight,ULONG IDCMP, UBYTE align, ...)
  561. {
  562.     struct Window    *Result;
  563.     va_list      VarArgs;
  564.  
  565.     va_start(VarArgs,align);
  566.  
  567.     Result = LT_Build(handle,
  568.         LAWN_Title,        title,
  569.         LAWN_Bounds,        bounds,
  570.         LAWN_ExtraWidth,    extraWidth,
  571.         LAWN_ExtraHeight,    extraHeight,
  572.         LAWN_IDCMP,        IDCMP,
  573.         LAWN_AlignWindow,    align,
  574.     TAG_MORE,(struct TagItem *)VarArgs);
  575.  
  576.     va_end(VarArgs);
  577.  
  578.     return(Result);
  579. }
  580.  
  581. struct Window * LIBENT
  582. LT_LayoutA(REG(a0) LayoutHandle *handle, REG(a1) STRPTR title,REG(a2) struct IBox *bounds,REG(d0) LONG extraWidth, REG(d1) LONG extraHeight,REG(d2) ULONG IDCMP, REG(d3) UBYTE align, REG(a3) struct TagItem *TagParams)
  583. {
  584.     return(LT_Build(handle,
  585.         LAWN_Title,        title,
  586.         LAWN_Bounds,        bounds,
  587.         LAWN_ExtraWidth,    extraWidth,
  588.         LAWN_ExtraHeight,    extraHeight,
  589.         LAWN_IDCMP,        IDCMP,
  590.         LAWN_AlignWindow,    align,
  591.     TAG_MORE,TagParams));
  592. }
  593.